* LAURITS F. AARSLEW, 2022, LFAA@PS.AU.DK

****************************

/*
This do-file reproduces tables and figures 
Tables are generated as Latex files (.tex), but - estout - can be used to generate other formats. 
Figures are saved as pdfs.  
*/

clear all
use "replication_data.dta", clear 
set scheme plotplain 


*** Figure 1
** Please note that the text in graphs are taken from the regression output

* PEI
quietly reg pei i.treatment if country == 0 & sample ==1, robust // DK
margins treatment
marginsplot, ///
   recast(bar) plotop(fcolor(white) lcolor(black)) ///
   xlabel(,nogrid) ylabel(1 2 3 4 5, nogrid) ytitle("Election Fairness") xtitle("") title("") legend(off) ///
   text(3.8 1 "Difference = -2.60***") text(3.6 1 "SE = 0.06") text(3.4 1 "P value < 0.001") name(dk_1)

quietly reg pei i.treatment if country == 1 & sample ==1, robust // MX
margins treatment
marginsplot, ///
   recast(bar) plotop(fcolor(white) lcolor(black)) ///
   xlabel(, nogrid) ylabel(1 2 3 4 5, nogrid) ytitle("Election Fairness") xtitle("") title("",) legend(off) ///
   text(3.8 1 "Difference = -1.23***") text(3.6 1 "SE = 0.07") text(3.4 1 "P value < 0.001") name(mx_1)

* Support
quietly reg support i.treatment if country == 0 & sample ==1, robust // DK
margins treatment
marginsplot, ///
   recast(bar) plotop(fcolor(white) lcolor(black)) ///
   xlabel(, nogrid) ylabel(1 2 3 4 5 6 7 8 9 10, nogrid) ytitle("Government Support") xtitle("") title("") legend(off) ///
   text(5.8 1 "Difference = -3.77***") text(5.4 1 "SE = 0.16") text(5 1 "P value < 0.001") name(dk_2)

quietly reg support i.treatment if country == 1 & sample ==1, robust // MX
margins treatment
marginsplot, ///
   recast(bar) plotop(fcolor(white) lcolor(black)) ///
   xlabel(, nogrid) ylabel(1 2 3 4 5 6 7 8 9 10, nogrid) ytitle("Government Support") xtitle("") title("") legend(off) ///
   text(5.8 1 "Difference = -2.00***") text(5.4 1 "SE = 0.17") text(5 1 "P value < 0.001") name(mx_2)

* Legitimacy
quietly reg legitimacy i.treatment if country == 0 & sample ==1, robust // DK
margins treatment
marginsplot, ///
   recast(bar) plotop(fcolor(white) lcolor(black)) ///
   xlabel(,nogrid) ylabel(1 2 3 4 5, nogrid) ytitle("Government Legitimacy") xtitle("") title("") legend(off) ///
   text(3.8 1 "Difference = -2.14***") text(3.6 1 "SE = 0.06") text(3.4 1 "P value < 0.001") name(dk_3)

quietly reg legitimacy i.treatment if country == 1 & sample ==1, robust // MX
margins treatment
marginsplot, ///
   recast(bar) plotop(fcolor(white) lcolor(black)) ///
   xlabel(,  nogrid) ylabel(1 2 3 4 5, nogrid) ytitle("Government Legitimacy") xtitle("") title("") legend(off) ///
   text(3.8 1 "Difference = -0.66***") text(3.6 1 "SE = 0.07") text(3.4 1 "P value < 0.001") name(mx_3)


graph combine dk_1 dk_2 dk_3, rows(1) name(dk_ate) title("Denmark") 
graph combine mx_1 mx_2 mx_3, rows(1) name(mx_ate) title("Mexico") 
graph combine dk_ate mx_ate, rows(2) name(fig1) plotregion(lcolor(black) lwidth(medium))

graph drop dk_1 dk_2 dk_3 mx_1 mx_2 mx_3 dk_ate mx_ate

*** FIGURE 2 
**PEI

quietly reg pei i.treatment##i.perp_support if country == 0 & sample ==1, robust // DK
margins, dydx(treatment) at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Election Fairness") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (.5) -3.5, nogrid) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==0, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small)) legend(off)) name(dk_1)
  
*Mexico
quietly reg pei i.treatment##i.perp_support if country == 1 & sample ==1, robust // MX
margins, dydx(treatment) at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Election Fairness") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (.5) -3.5, nogrid) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==1, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small)) legend(off)) name(mx_1)


***Government Support
*Denmark
quietly reg support i.treatment##i.perp_support if country == 0 & sample==1, robust // DK
margins, dydx(treatment) at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Government Support") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (.5) -3.5, nogrid) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==0, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small)) legend(off)) name(dk_2)

*Mexico

quietly reg support i.treatment##i.perp_support if country == 1 & sample ==1, robust // MX
margins, dydx(treatment) at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Government Support") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (.5) -3.5, nogrid) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==1, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small)) legend(off)) name(mx_2)

***Legitimacy
*Denmark
quietly reg legitimacy i.treatment##i.perp_support if country == 0 & sample ==1, robust // DK
margins, dydx(treatment) at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Government Legitimacy") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (.5) -3.5, nogrid) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==0, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small)) legend(off)) name(dk_3)

*Mexico
quietly reg legitimacy i.treatment##i.perp_support if country == 1 & sample ==1, robust // MX
margins, dydx(treatment) at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Government Legitimacy") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (.5) -3.5, nogrid) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==1, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small)) legend(off)) name(mx_3)

** To generate Figure 2, follow the same steps at with Figure 1 (above). First combine the three graphs for Denmark, then combine the three graphs for Mexico. Finally, combine these "combined" graphs. The hight of the histograms have been squeesed manually and the colorscheme has been changed. 

graph combine dk_1 dk_2 dk_3, rows(1) name(dk_marg) title("Denmark") 
graph combine mx_1 mx_2 mx_3, rows(1) name(mx_marg) title("Mexico") 
graph combine dk_marg mx_marg, rows(2) name(fig2) plotregion(lcolor(black) lwidth(medium))

graph drop dk_1 dk_2 dk_3 mx_1 mx_2 mx_3 dk_marg mx_marg

*** FIGURE 3
** Please note that the dotted line is taken from margins output (it correspond to the predicted margin for strong co-partisans). 

*** DENMARK
*Support
quietly reg support i.treatment##i.perp_support if country == 0 & sample==1, robust // DK
margins treatment, at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Government Support") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (1) 10, nogrid) ///
  yline(4.369318) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==0, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small)) legend(off)) name(dk_1)

*Legitimacy  
quietly reg legitimacy i.treatment##i.perp_support if country == 0 & sample==1, robust // DK
margins treatment, at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Government Legitimacy") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (1) 10, nogrid) ///
  yline(2.642045) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==0, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small)) legend(off)) name(dk_2)

*graph combine "CDIRECTORY" "DIRECTORY", rows(1)
  
*** MEXICO
*Support  
quietly reg support i.treatment##i.perp_support if country == 1 & sample==1, robust // MEX
margins treatment, at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Government Support") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (1) 10, nogrid) ///
  yline(5.913793) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==1, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small))) name(mx_1)


*Legitimacy
quietly reg legitimacy i.treatment##i.perp_support if country == 1 & sample==1, robust // DK
margins treatment, at(perp_support=(1(1)5))
marginsplot, ///
  recast(scatter) ///
  plotop(fcolor(white) lcolor(black) mlabel(_margin) mlabformat(%5.2fc) mlabsize(vsmall)) ///
  title("") ///
  xtitle("") ytitle("Government Legitimacy") ///
  xlabel(1 "Strong Out-partisan" 3 "Neutral" 5 "Strong Co-partisan", nogrid) ///
  ylabel(0 (1) 10, nogrid) ///
  yline(3.126437) ///
  graphregion(color(white)) ///
  addplot(hist perp_support if country==1, discrete yaxis(2) yscale(off axis(2)) fcolor(gs10%10) lcolor(black%50 size(small))) name(mx_2)


  
** To generate Figure 3, follow the same steps at with Figure 1 and Figure 2 (above). First combine the two graphs for Denmark, then combine the two graphs for Mexico. Finally, combine these "combined" graphs. The hight of the histograms have been squeesed manually and the colorscheme has been changed.  
graph combine dk_1 dk_2, rows(1) name(dk_marg) title("Denmark") 
graph combine mx_1 mx_2, rows(1) name(mx_marg) title("Mexico") 
graph combine dk_marg mx_marg, rows(2) name(fig3) plotregion(lcolor(black) lwidth(medium))


